#include <algorithm>
using namespace std;
#include <vector>
#include <queue>

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    explicit TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution
{
public:
    static int widthOfBinaryTree(TreeNode* root)
    {
        unsigned int ret = 0;
        if(root == nullptr)
            return ret;
        vector<pair<TreeNode*, unsigned int>> q;
        q.push_back({root, 1});
        while(!q.empty())
        {
            auto [x1, y1] = q[0];
            auto [x2, y2] = q.back();
            ret = max(ret, y2-y1+1);//max 函数要求两个参数的类型必须一致。所以ret也要unsigned int
            vector<pair<TreeNode*, unsigned int>> tmp;//设置它是因为不能直接在q中push_back，会一直插入，遍历不完。q只遍历当前层。
            for(auto [x,y] : q)
            {
                if(x->left)
                    tmp.push_back({x->left, 2*y});
                if(x->right)
                    tmp.push_back({x->right, 2*y+1});
            }
            q = tmp;
        }
        return ret;
    }
};